/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package consumo.session;

import consumo.ejb.BasicSessionBean;
import consumo.entity.Concessionaria;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author Carlos Taschetto
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ConcessionariaSession extends BasicSessionBean{

    public Concessionaria getConcessionariaById(int conceId) {
        return getPojo(Concessionaria.class, conceId);
    }

    public List<Concessionaria> getAllConcessionarias(){
        return  getList(Concessionaria.class, "Select c from Concessionaria c");
    }

    public Concessionaria getConcessionariaByNome(String nome){
        return getPojo(Concessionaria.class, "select c from Concessionaria c where c.nome like ?1", "%"+nome+"%");
    }

    public Concessionaria getConcessionariaByAbreviatura(String nome){
        return getPojo(Concessionaria.class, "select u from Concessionaria u where u.abrev like ?1", "%"+nome+"%");
    }

    /**
     * Insere uma nova Concessionaria
     * @param object
     * @return object
     */
    public Concessionaria saveConcessionaria(Concessionaria object){
        getEm().persist(object);
        return object;
    }

    /**
     * Altera dados do Concessionaria
     * @param object
     * @return user editado
     */
    public Concessionaria setConcessionaria(Concessionaria object){
        getEm().merge(object);
        return object;
    }

    public boolean removeConcessionaria(int id){
        return (execute("Delete from Concessionaria u where u.id = ?1", id) <= 0);
    }

    public void removeConcessionaria(Concessionaria object){
        removeEntity(object);
    }
}
